package com.moody.init;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.moody.hibernate.domain.CodeLib;


/*
 * Copyright © Moody analytics
 *
 * @Author: Yan Jian
 * @Time: 2011-9-14 上午11:26:46
 * @Modified by: 
 * @Description: 国家初始数据处理
 */
public class Country {
	private static final Logger log = LoggerFactory.getLogger(DataDictionary.class);
	private static Country instance = null;
	private static Map<String, String> countryMap = new LinkedHashMap<String, String>();

	private Country() {

	}

	public static Country getInstance() {
		if (instance == null) {
			instance = new Country();
		}
		return instance;
	}
	
	public  Map<String, String> getCountry(){
		
		Map<String, CodeLib> country = new LinkedHashMap<String, CodeLib>();
		
		country=DataDictionary.getInstance().getDataDictionary().get("CountryCode");
		
		
		Iterator iter = country.entrySet().iterator(); 
		while (iter.hasNext()) { 
		    Map.Entry entry = (Map.Entry) iter.next(); 
		    String countryCode = (String) entry.getKey(); 
		    String countryName = ((CodeLib)entry.getValue()).getItemName();;
		    if (countryCode.length() == 3){
		    	countryMap.put(countryCode, countryName);
		    	
		    }    
		}
//		log.info("Region: ", regionMap);
		return countryMap;
	}
	
	public  JSONArray getCountryJSON(){
		
		Map<String, CodeLib> country = new LinkedHashMap<String, CodeLib>();
		
		country=DataDictionary.getInstance().getDataDictionary().get("CountryCode");
		Map<String, String> countryM = new LinkedHashMap<String, String>();
		Map<String, Object> countryMT = new LinkedHashMap<String, Object>();
		
		List cl = new ArrayList();
		Iterator iter = country.entrySet().iterator(); 
		while (iter.hasNext()) { 
		    Map.Entry entry = (Map.Entry) iter.next(); 
		    String countryCode = (String) entry.getKey(); 
		    String countryName = ((CodeLib)entry.getValue()).getItemName();;
		    if (countryCode.length() == 3){
		    	countryM.put(countryCode, countryName);
		    	String s = "{\"code\":"+"\""+countryCode+"\""+",\"name\":"+"\""+countryName+"\""+"}";
		    	JSONObject js = JSONObject.fromObject(s);
		    	cl.add(js);
		    }    
		}
		/*countryMT.put("total", countryM.size());
		countryMT.put("country", cl);*/
//		log.info("Region: ", regionMap);
		JSONArray jo = JSONArray.fromObject(cl);
		return jo;
	}

}
